// const BASE_URL = 'http://192.168.8.201:8080'  //测试
// const BASE_URL = 'http://192.168.8.211:8081'
// const BASE_URL = 'http://192.168.8.4:8081'
const BASE_URL = 'http://192.168.8.10:8081'  //正式
// const BASE_URL = 'http://api.thwater.com:8081'
// const BASE_URL = 'http://192.168.19.210:8080'
// const BASE_URL = 'http://192.168.19.210:8080'
// const BASE_URL = 'http://192.168.19.218:8080' // lc
// const BASE_URL = 'http://192.168.19.117:8081' // zxl

let _J = {
	'X-Requested-With': 'XMLHttpRequest',
	"Accept": "application/json",
	"Accept-Language": "zh-CN",
	"Content-Type": "application/json; charset=UTF-8",
}
let _F = {
	'X-Requested-With': 'XMLHttpRequest',
	'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
}
// import store from '../../store/store'

function againTip(content, callbcak, opts, resolve, reject) {
	uni.showModal({
		title: '提示',
		content,
		showCancel: false,
		success: function (res) {
			if (res.confirm) {
				callbcak(opts, resolve, reject)
			} else {
				reject()
			}
		}
	})
}


function failTip(title, content, reject) {
	uni.showModal({
		title,
		content,
		showCancel: false,
		complete() {
			reject()
		}
	})
}

function request(opts, resolve, reject) {
	const tenantCode = uni.getStorageSync("tenantCode") || "";
	const tenantCodeObj = {
		tenantCode: tenantCode
	};
	_F.tenantCode = tenantCode;
	_J.tenantCode = tenantCode;
	//菜单id
	const menuId = uni.getStorageSync("menuId") || "";
	_F.menuId = menuId;
	_J.menuId = menuId;


	const configType = opts.type == 'form' ? _F : _J;
	const reqHeader = Object.assign(configType, opts.needToken ? {
		'Authorization': "Bearer " +  uni.getStorageSync(
			'storage_token')
	} : {
		'Authorization': "Basic dGh3YXRlci1hcHA6dGh3YXRlci1hcHA= "
	})
	uni.request({
		url: BASE_URL + opts.url,
		// url:
		//   opts.url.indexOf("/safe") === 0 && process.env.NODE_ENV === "development"
		//     ? opts.url
		//     : BASE_URL + opts.url,
		data: opts.data,
		method: opts.method,
		header: reqHeader,
		timeout: opts.timeout || 600000,
		success(res) {
			// console.log("---request",JSON.stringify(opts.data),opts.url,reqHeader,res.data)
			if (res.statusCode === 200) {
				if (res.data.code === 401) {
					uni.removeStorageSync('storage_token')
					uni.showModal({
						title: '提示',
						content: '登录已过期，请重新登录',
						showCancel: false,
						success(res) {
							if (res.confirm) {
								uni.reLaunch({
									url: "/pages/login/login",
									success: () => {
										plus.navigator.closeSplashscreen();
									},
									fail: (err) => {
										console.log("登录过期401跳转页面error", err)
									}
								})
							}
						}
					})
				} else {
					resolve(res.data)
				}
			} else {
				console.log(res)
				reject(res.data)
			}
		},
		fail(err) {
			reject(err)
			// againTip(`${opts.tip}失败了，请检查网络，然后点击确认重试`,request,opts,resolve,reject)
		}
	})
}

function upload(opts, resolve, reject) {
	uni.uploadFile({
		url: BASE_URL + opts.url,
		filePath: opts.data,
		name: "file",
		timeout: opts.timeout || 30000,
		header: {
			Accept: "application/json, text/plain, */*",
			tenantCode: uni.getStorageSync("tenantCode") || "",
			Authorization: "Bearer " + (store.state._TOKEN || uni.getStorageSync("storage_token")),
		},
		success(res) {
			let resData = {}
			if (typeof res.data === "string") {
				try {
					resData = JSON.parse(res.data);
				} catch {
					resData = res.data;
				}
			}
			if (resData.code === 0) {
				resolve(resData);
			} else if (resData.code === 401) {
				uni.removeStorageSync("storage_token");
				uni.showModal({
					title: "提示",
					content: "登录已过期，请重新登录",
					showCancel: false,
					success(res) {
						if (res.confirm) {
							uni.reLaunch({
								url: "/pages/login/login",
								success: () => {
									plus.navigator.closeSplashscreen();
								},
							});
						}
					},
				});
			} else {
				reject(resData);
			}
		},
		fail(err) {
			reject(err);
			// againTip(`${opts.tip}失败了，请检查网络，然后点击确认重试`,request,opts,resolve,reject)
		},
	});
}

const baseRequest = (opts) => {
	return new Promise((resolve, reject) => {
		request(opts, resolve, reject)
	})
}

const baseUpload = (opts) => {
	return new Promise((resolve, reject) => {
		upload(opts, resolve, reject);
	})
}

export {
	baseRequest,
	baseUpload,
	BASE_URL
}